package com.tarena.flight_game;
/**
 * 敌机类
 * @author tang
 *
 */
public class Enemy {
  int x;
  int y;
  int r;//几率
  int f;//开火几率
  int vx;//速度
  int vy;//速度
  int cont;//生命周期
  boolean islive = true ;
  boolean toleft = false ;
  boolean toright = false ;
  
  public Enemy(int x,int vy,int r,int f){
  	this.x = x;
  	this.y = -50;
  	this.vx = 0;
  	this.vy = vy;
  	this.r = r;
  	this.f = f;
  	this.cont = 0;
  }
  
  /**
   * 敌机移动
   * @param a
   */
  public void move(int a){
  	// 一定几率横向移动
  	if(Math.random()*100 > r){
  	   if (a - this.x > 50 && cont == 0){
  	   	  this.vx = 3; 
  	   	  this.toright = true;
  	   	  this.toleft = false;
  	   }
  	   else if (this.x - a > 50 && cont == 0){
  	   	  this.vx = -3;
  	   	  this.toright = false;
  	   	  this.toleft = true;
  	   }
  	   this.cont++;
    }
    if (this.cont > 2 ){
  	    this.vx = 0;
  	    this.toright = false;
  	    this.toleft = false;
  	    this.cont = 0;
  	}
  	this.x += this.vx;
  	this.y += this.vy;
  }
  
  /**
   * 敌机开火
   * @return
   */
   public boolean tofire(){
  	// 一定几率开火
  	if(Math.random()*100 > f){ return true; }  	
  	else { return false;}
  }
   
  /**
   * 碰撞检查
   * @param a
   * @param b
   */
  public void hit(int a,int b){
  	 if ( Math.abs((x + 25) - (a + 25)) < 28 && Math.abs((y + 25) - (b + 25)) < 28)
  	      {this.islive = false;}
  	 else {this.islive = true;}
  }  
}
